home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / µSim 1.1 / source / Animation.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-25  |  11.8 KB  |  454 lines  |  [TEXT/CWIE]

  1. /*
  2. Copyright © 1993-1997 Fabrizio Oddone
  3. ••• ••• ••• ••• ••• ••• ••• ••• ••• •••
  4. This source code is distributed as freeware:
  5. you may copy, exchange, modify this code.
  6. You may include this code in any kind of application: freeware,
  7. shareware, or commercial, provided that full credits are given.
  8. You may not sell or distribute this code for profit.
  9. */
  10.  
  11.  
  12. #include    "UtilsSys7.h"
  13. #include    "Conversions.h"
  14. #include    "FabWList.h"
  15. #include    "Independents.h"
  16. #include    "SimResIDs.h"
  17.  
  18. #include    "Globals.h"
  19. #include    "Animation.h"
  20. #include    "ControlStore.h"
  21. #include    "DoEditDialog.h"
  22. #include    "DoMenu.h"
  23. #include    "Main.h"
  24. #include    "Registers.h"
  25.  
  26. #if defined(FabSystem7orlater)
  27.  
  28.  
  29. static GrafPtr    sOffScr;        /* offscreen GrafPort for animation */
  30. static PicHandle    sImages[kLAST_PICT - kFIRST_PICT + 1];    /* Handles to Pictures */
  31. static Rect    sRval[kP_MIR - kP_ALATCH + kMIRSubboxes];    /* in these rects we write the values */
  32. static RgnHandle    sMirUpdRgn;
  33.  
  34.  
  35.  
  36. static Boolean PtInObj(Point, Rect * const, short obj);
  37.  
  38.  
  39. #pragma segment Init
  40. /* Init_Animation: initializes the Animation Window */
  41.  
  42. OSErr Init_Animation(void)
  43. {
  44. static const short    initial[] = { kP_REGISTERS, kC_ALU2MSL1, kC_ALU2MSL2, kP_MIR, kP_TEXT, 0 };
  45.  
  46. Rect    tempRect;
  47. BitMap    offBits;
  48. FabWindowPtr    thefabw;
  49. WindowPtr    w;
  50. PicHandle    tempPH;
  51. RgnHandle    theRgn;
  52. short    rightLong;
  53. short    count, i;
  54. OSErr    err = appMemFullErr;
  55.  
  56. gWPtr_Animation = w = GetNewWindow(kWIND_Animation, nil, (WindowPtr)-1L);
  57. if (w) {
  58.     SetPortWindowPort(w);
  59.     if (gPrefs.remembWind)
  60.         if (IsOnScreenWeak(gPrefs.AnimTopLeft))
  61.             MoveWindow(w, gPrefs.AnimTopLeft.h, gPrefs.AnimTopLeft.v, false);
  62.     ClipRect(&w->portRect);
  63.     offBits.bounds = w->portRect;
  64.     rightLong = ((offBits.bounds.right + 15) >> 4) << 1;
  65.     offBits.rowBytes = rightLong;
  66.     if (sOffScr = (GrafPtr)NewPtrClear(sizeof(GrafPort))) {
  67.         OpenPort(sOffScr);
  68.         if (offBits.baseAddr = NewPtrClear(rightLong * (long)offBits.bounds.bottom)) {
  69.             SetPortBits(&offBits);
  70.             TextFont(monaco);
  71.             ClipRect(&offBits.bounds);
  72.             PenPat(&qd.gray);
  73.             PenMode(notPatBic);
  74.             sMirUpdRgn = NewRgn();
  75.                 {
  76.                 register RgnHandle tempRgn = NewRgn();
  77.                 register PicHandle *PictScan;
  78.                 register RectPtr    RectScan;
  79.             
  80.                 for (RectScan = sRval, PictScan = sImages, count = kFIRST_PICT;
  81.                     count <= kLAST_PICT; count++) {
  82.                     register PicHandle tempPicH;
  83.                     
  84.                     tempPicH = GetPicture(count);
  85.                     *PictScan++ = tempPicH;
  86.                     tempRect = (*tempPicH)->picFrame;
  87.                     DrawPicture(tempPicH, &tempRect);
  88.                     RectRgn(tempRgn, &tempRect);
  89.                     PaintRgn(tempRgn);
  90.                     if( count <= kP_MPC ) {
  91.                         --tempRect.bottom;
  92.                         --tempRect.right;
  93.                         InsetRect(&tempRect, 3, 3);
  94.                         *RectScan++ = tempRect;
  95.                         }
  96.                     else if( count == kP_ALU ) {
  97.                         tempRect.bottom -= 4;
  98.                         tempRect.right -= 15;
  99.                         tempRect.left += 15;
  100.                         tempRect.top = tempRect.bottom - 15;
  101.                         *RectScan++ = tempRect;
  102.                         }
  103.                     else if( count == kP_MSL ) {
  104.                         tempRect.bottom -= 3;
  105.                         tempRect.top += 3;
  106.                         tempRect.right -= 3;
  107.                         tempRect.left += 19;
  108.                         *RectScan++ = tempRect;
  109.                         }
  110.                     else if( count == kP_MIR ) {
  111.                         register int    ind;
  112.             
  113.                         tempRect.bottom -= 2;
  114.                         ++tempRect.left;
  115.                         tempRect.top = tempRect.bottom - 15;
  116.                         tempRect.right = tempRect.left + 11;
  117.                         for( ind = 0; ind <= 12; ind++) {
  118.                             *RectScan++ = tempRect;
  119.                             RectRgn(tempRgn, &tempRect);
  120.                             UnionRgn(sMirUpdRgn, tempRgn, sMirUpdRgn);
  121.                             tempRect.left += 12;
  122.                             tempRect.right += 12;
  123.                             }
  124.                         tempRect.right += 11;
  125.                         *RectScan = tempRect;
  126.                         RectRgn(tempRgn, &tempRect);
  127.                         UnionRgn(sMirUpdRgn, tempRgn, sMirUpdRgn);
  128.                         }
  129.                     }
  130.                 DisposeRgn(tempRgn);
  131.                 }
  132.             ChangedAllBoxes();
  133.             ActivateObjs(initial);
  134.  
  135.             thefabw = AddWindowToList(w);
  136.             SetUpdate(thefabw, Update_Animation);
  137.             SetGoAway(thefabw, CloseAnimation);
  138.             SetContent(thefabw, Do_Animation);
  139.  
  140.             SetPortWindowPort(w);
  141.             for (i = kP_ALATCH - kFIRST_PICT; i <= kP_MAP - kFIRST_PICT; i++) {
  142.                 theRgn = NewRgn();
  143.                 tempPH = sImages[i];
  144.                 if (*tempPH == nil)
  145.                     LoadResource((Handle)tempPH);
  146.                 tempRect = (*tempPH)->picFrame;
  147.                 RectRgn(theRgn, &tempRect);
  148.                 InstallRgnHandler(thefabw, theRgn, nil, nil,
  149.                             toMenu(kBalloons_Anim, i + 1), 6, 0);
  150.                 }
  151. // there are no resizable objects in this window
  152.         //    ResizeObjects(w);
  153.         
  154.             RecalcGlobalCoords(thefabw);
  155.  
  156.             err = noErr;
  157.             }
  158.         }
  159.     }
  160. return err;
  161. }
  162.  
  163.  
  164. #pragma segment Main
  165. /*
  166. PtInObj: finds out if thePt is in the obj object, returning the object
  167. Rect in neededRect */
  168.  
  169. static Boolean PtInObj(Point thePt, Rect * const neededRect, short obj)
  170. {
  171. register PicHandle    tempPH;
  172.  
  173. tempPH = sImages[obj];
  174. if (*tempPH == nil)    // handle was purged
  175.     LoadResource((Handle)tempPH);
  176. *neededRect = (*tempPH)->picFrame;
  177. neededRect->right--;
  178. neededRect->bottom--;
  179. return(PtInRect(thePt, neededRect));
  180. }
  181.  
  182. /*
  183. UpdateMir: takes care of updating the mir image in the Animation window */
  184.  
  185. void UpdateMir(un_mir newmir)
  186. {
  187. Str255    tempS;
  188. GrafPtr    savePort;
  189. register RectPtr    RectScan = &sRval[kP_MIR - kP_ALATCH];
  190.  
  191. GetPort(&savePort);
  192. SetPort(sOffScr);
  193. MyNumToString((newmir).bits.amux, tempS);
  194. TETextBox(&tempS[1], StrLength(tempS), RectScan++, teJustCenter);
  195. MyNumToString((newmir).bits.cond, tempS);
  196. TETextBox(&tempS[1], StrLength(tempS), RectScan++, teJustCenter);
  197. MyNumToString((newmir).bits.alu, tempS);
  198. TETextBox(&tempS[1], StrLength(tempS), RectScan++, teJustCenter);
  199. MyNumToString((newmir).bits.shift, tempS);
  200. TETextBox(&tempS[1], StrLength(tempS), RectScan++, teJustCenter);
  201. MyNumToString((newmir).bits.mbr, tempS);
  202. TETextBox(&tempS[1], StrLength(tempS), RectScan++, teJustCenter);
  203. MyNumToString((newmir).bits.rd, tempS);
  204. TETextBox(&tempS[1], StrLength(tempS), RectScan++, teJustCenter);
  205. MyNumToString((newmir).bits.wr, tempS);
  206. TETextBox(&tempS[1], StrLength(tempS), RectScan++, teJustCenter);
  207. MyNumToString((newmir).bits.mar, tempS);
  208. TETextBox(&tempS[1], StrLength(tempS), RectScan++, teJustCenter);
  209. MyNumToString((newmir).bits.dsc, tempS);
  210. TETextBox(&tempS[1], StrLength(tempS), RectScan++, teJustCenter);
  211. ShortToHexString((newmir).bits.a, tempS);
  212. TETextBox(&tempS[4], 1, RectScan++, teJustCenter);
  213. ShortToHexString((newmir).bits.b, tempS);
  214. TETextBox(&tempS[4], 1, RectScan++, teJustCenter);
  215. ShortToHexString((newmir).bits.c, tempS);
  216. TETextBox(&tempS[4], 1, RectScan++, teJustCenter);
  217. MyNumToString((newmir).bits.map, tempS);
  218. TETextBox(&tempS[1], StrLength(tempS), RectScan++, teJustCenter);
  219. MyNumToString((newmir).bits.addr, tempS);
  220. TETextBox(&tempS[1], StrLength(tempS), RectScan++, teJustRight);
  221. SetPort(gWPtr_Animation);
  222. InvalRgn(sMirUpdRgn);
  223. SetPort(savePort);
  224. }
  225.  
  226. /* ChangedBox: does all the housekeeping when an on-screen object is modified */
  227.  
  228. void ChangedBox(int obj)
  229. {
  230. Str255    tempS;
  231. GrafPtr    savePort;
  232.  
  233. GetPort(&savePort);
  234. SetPort(sOffScr);
  235. ShortToHexString(gParts[obj], tempS);
  236. if((obj >= kP_INCR - kFIRST_PICT)&&(obj <= kP_MPC - kFIRST_PICT)) {
  237.     *(Byte *)(&gParts[obj]) = 0;
  238.     MyNumToString(gParts[obj], tempS);
  239.     }
  240. else {
  241.     if( obj == kP_ALU - kFIRST_PICT ) {
  242.         register unsigned char *mslstring = (unsigned char *)&tempS + 8;
  243.         register StringPtr ZeroandOne = *GetString(kSTR_BINALLOWED);
  244.  
  245.         *mslstring++ = (gParts[kP_ALU - kFIRST_PICT] < 0 ? *(ZeroandOne+2) : *(ZeroandOne+1));
  246.         *mslstring++ = 13;
  247.         *mslstring++ = (gParts[kP_ALU - kFIRST_PICT] == 0 ? *(ZeroandOne+2) : *(ZeroandOne+1));
  248.         TextSize(9);
  249.         TETextBox((unsigned char *)&tempS + 8, 3, &sRval[kP_MSL - kFIRST_PICT], teJustCenter);
  250.         TextSize(12);
  251.         }
  252.     }
  253. TETextBox(&tempS[1], StrLength(tempS), &sRval[obj], teJustCenter);
  254. SetPort(gWPtr_Animation);
  255. InvalRect(&sRval[obj]);
  256. if (obj == kP_ALU - kFIRST_PICT)
  257.     InvalRect(&sRval[kP_MSL - kFIRST_PICT]);
  258. SetPort(savePort);
  259. }
  260.  
  261. void ChangedAllBoxes(void)
  262. {
  263. register int    i;
  264.  
  265. for (i = kP_ALATCH - kFIRST_PICT; i <= kP_ALU - kFIRST_PICT; ChangedBox(i++))
  266.     ;
  267. }
  268.  
  269. /* ActivateObjs: activates (blackens) a list of objects */
  270.  
  271. void ActivateObjs(const short *objs)
  272. {
  273. Rect    tempRect;
  274. GrafPtr    savePort;
  275. short    pos;
  276.  
  277. GetPort(&savePort);
  278. SetPort(sOffScr);
  279. PenNormal();
  280.  
  281. for( ; pos = *objs++; ) {
  282.     register PicHandle    tempPicH;
  283.     
  284.     tempPicH = sImages[pos - kFIRST_PICT];
  285.     if( *tempPicH == nil ) /* Resource purged */
  286.         LoadResource((Handle)tempPicH);
  287.     tempRect = (*tempPicH)->picFrame;
  288.     SetPort(sOffScr);
  289.     DrawPicture(tempPicH, &tempRect);
  290.     SetPort(gWPtr_Animation);
  291.     InvalRect(&tempRect);
  292.     }
  293. SetPort(savePort);
  294. }
  295.  
  296. /* DeactivateObjs: deactivates (grays) a list of objects */
  297.  
  298. void DeactivateObjs(const short *objs)
  299. {
  300. Rect    tempRect;
  301. GrafPtr    savePort;
  302. RgnHandle    tempRgn = NewRgn();
  303. RgnHandle    tempRgn2 = NewRgn();
  304. short    pos;
  305.  
  306. GetPort(&savePort);
  307. SetPort(sOffScr);
  308. PenPat(&qd.gray);
  309. PenMode(notPatBic);
  310.  
  311. for( ; pos = *objs++; ) {
  312.     register PicHandle    tempPicH;
  313.     
  314.     tempPicH = sImages[pos - kFIRST_PICT];
  315.     if( *tempPicH == nil ) /* Resource purged */
  316.         LoadResource((Handle)tempPicH);
  317.     tempRect = (*tempPicH)->picFrame;
  318.     SetPort(sOffScr);
  319.     DrawPicture(tempPicH, &tempRect);
  320.     tempRect.right--;
  321.     tempRect.bottom--;
  322.     if(pos <= kP_MIR) {
  323.         if(pos == kP_MIR) {
  324.             CopyRgn(sMirUpdRgn, tempRgn2);
  325.             InvertRgn(sMirUpdRgn);
  326.             }
  327.         else
  328.             RectRgn(tempRgn2, &sRval[pos - kP_ALATCH]);
  329.         RectRgn(tempRgn, &tempRect);
  330.         DiffRgn(tempRgn, tempRgn2, tempRgn);
  331.         }
  332.     else
  333.         RectRgn(tempRgn, &tempRect);
  334.     PaintRgn(tempRgn);
  335.     SetPort(gWPtr_Animation);
  336.     InvalRect(&tempRect);
  337.     }
  338. DisposeRgn(tempRgn2);
  339. DisposeRgn(tempRgn);
  340. SetPort(savePort);
  341. }
  342.  
  343. /* Update_Animation: handler for the update event */
  344.  
  345. void Update_Animation(WindowPtr w, short)
  346. {
  347.  
  348. CopyBits(&sOffScr->portBits, &w->portBits, &sOffScr->portBits.bounds,
  349.             &w->portRect, srcCopy, nil);
  350. }
  351.  
  352. /* Do_Animation: someone has clicked in … */
  353.  
  354. void Do_Animation(WindowPtr /*w*/, EventRecord *theEvent)
  355. {
  356. //#pragma unused (w)
  357.  
  358. Rect    tempRect;
  359. Point    myPt;
  360. register short    code;
  361.  
  362. myPt = theEvent->where;
  363. GlobalToLocal(&myPt);
  364. if (PtInObj(myPt, &tempRect, code = kP_ALATCH - kFIRST_PICT) ||
  365.     PtInObj(myPt, &tempRect, code = kP_BLATCH - kFIRST_PICT) ||
  366.     PtInObj(myPt, &tempRect, code = kP_MAR - kFIRST_PICT) ||
  367.     PtInObj(myPt, &tempRect, code = kP_MBR - kFIRST_PICT) ||
  368.     PtInObj(myPt, &tempRect, code = kP_AMUX - kFIRST_PICT) ||
  369.     PtInObj(myPt, &tempRect, code = kP_ALU - kFIRST_PICT) ||
  370.     PtInObj(myPt, &tempRect, code = kP_SHIFTER - kFIRST_PICT) ||
  371.     PtInObj(myPt, &tempRect, code = kP_MMUX - kFIRST_PICT) ||
  372.     PtInObj(myPt, &tempRect, code = kP_INCR - kFIRST_PICT) ||
  373.     PtInObj(myPt, &tempRect, code = kP_MPC - kFIRST_PICT) ||
  374.     PtInObj(myPt, &tempRect, code = kP_REGISTERS - kFIRST_PICT) ||
  375.     (PtInObj(myPt, &tempRect, code = kP_CONTSTORE - kFIRST_PICT) && DocIsOpen()) ) {
  376.     if (TrackObject(&tempRect)) {
  377.         if (code == kP_REGISTERS - kFIRST_PICT)
  378.             DoMenuWindows(kMItem_Registers);
  379.         else if (code == kP_CONTSTORE - kFIRST_PICT)
  380.             DoMenuWindows(kMItem_Microprogram);
  381.         else {
  382.             (void)DoEditDialog(code, kPART_NAMES,
  383.                                 (code >= kP_INCR - kFIRST_PICT) &&
  384.                                     (code <= kP_MPC - kFIRST_PICT) ? kPOP_DEC : kPOP_HEX);
  385.             UnloadSeg(DoEditDialog);
  386.             }
  387.         }
  388.     }
  389. }
  390.  
  391. /* TrackObject: like all the TrackThing in the Mac OS */
  392.  
  393. Boolean    TrackObject(RectPtr    r)
  394. {
  395. Point    myPt;
  396. register Boolean    inrect;
  397.  
  398. InvertRect(r);
  399. inrect = true;
  400. do {
  401.     register Boolean tempB;
  402.  
  403.     GetMouse(&myPt);
  404.     if ((tempB = PtInRect(myPt, r)) != inrect) {
  405.         InvertRect(r);
  406.         inrect = tempB;
  407.         }
  408.     }
  409. while( StillDown() );
  410. if (inrect) {
  411.     InvertRect(r);
  412.     }
  413. return inrect;
  414. }
  415.  
  416. /* procedure called when closing the Animation window */
  417.  
  418. void CloseAnimation(WindowPtr w)
  419. {
  420. DoCloseWindow(w, kMItem_Animation);
  421. }
  422.  
  423. /* ZoomEffectMap2MPC: the hardware decoding unit is modifying
  424. the Micro Program Counter; let the user have a visual feedback */
  425.  
  426. void ZoomEffectMap2MPC(void)
  427. {
  428. GrafPtr    savePort;
  429. Rect    mapRect, mpcRect;
  430. PicHandle    tempPH;
  431.  
  432. GetPort(&savePort);
  433. SetPort(gWPtr_Animation);
  434. tempPH = sImages[kP_MAP - kFIRST_PICT];
  435. if (*tempPH == nil)
  436.     LoadResource((Handle)tempPH);
  437. mapRect = (*tempPH)->picFrame;
  438. tempPH = sImages[kP_MPC - kFIRST_PICT];
  439. if (*tempPH == nil)
  440.     LoadResource((Handle)tempPH);
  441. mpcRect = (*tempPH)->picFrame;
  442.  
  443. LocalToGlobal(&topLeft(mapRect));
  444. LocalToGlobal(&botRight(mapRect));
  445. LocalToGlobal(&topLeft(mpcRect));
  446. LocalToGlobal(&botRight(mpcRect));
  447.  
  448. ZoomingRects(&gPrefs.zfr, &mapRect, &mpcRect, zoomDecelerate);
  449. SetPort(savePort);
  450. }
  451.  
  452. #endif
  453.  
  454.